#include "startingpoint_C8.h"

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include <highgui.h>

#define BLACK_IMAGE "data/black.bmp"
#define WHITE_IMAGE "data/white.bmp"
#define WHITECORNERS_IMAGE "data/whiteCorners.bmp"
#define BLACKCORNERS_IMAGE "data/blackCorners.bmp"
#define GREYCORNERS_IMAGE "data/greyCorners.bmp"

int main (int argc, char **argv){
	string waitForKey;
	CvPoint startingPoint;
	CvPoint noMeaningPoint;
	CvPoint initialPoint;
	CvPoint corner_0_99;
	noMeaningPoint.x = noMeaningPoint.y = 100;
	initialPoint.x = initialPoint.y = 0;
	corner_0_99.x = 99;
	corner_0_99.y = 0;
	IplImage* blackImage = cvLoadImage(BLACK_IMAGE,IPL_DEPTH_8U);
	IplImage* blackImage_gs = cvCreateImage(cvGetSize(blackImage), IPL_DEPTH_8U , 1);
	cvCvtColor(blackImage , blackImage_gs, CV_RGB2GRAY);
	IplImage* whiteImage = cvLoadImage(WHITE_IMAGE,IPL_DEPTH_8U); 
	IplImage* whiteImage_gs = cvCreateImage(cvGetSize(whiteImage), IPL_DEPTH_8U , 1);
	cvCvtColor(whiteImage , whiteImage_gs, CV_RGB2GRAY);
	IplImage* whiteCornersImage = cvLoadImage(WHITECORNERS_IMAGE,IPL_DEPTH_8U);
	IplImage* whiteCornersImage_gs = cvCreateImage(cvGetSize(whiteCornersImage), IPL_DEPTH_8U , 1);
	cvCvtColor(whiteCornersImage , whiteCornersImage_gs, CV_RGB2GRAY);
	IplImage* blackCornersImage = cvLoadImage(BLACKCORNERS_IMAGE,IPL_DEPTH_8U);
	IplImage* blackCornersImage_gs = cvCreateImage(cvGetSize(blackCornersImage), IPL_DEPTH_8U , 1);
	cvCvtColor(blackCornersImage , blackCornersImage_gs, CV_RGB2GRAY);
	IplImage* greyCornersImage = cvLoadImage(GREYCORNERS_IMAGE,IPL_DEPTH_8U);
	IplImage* greyCornersImage_gs = cvCreateImage(cvGetSize(greyCornersImage), IPL_DEPTH_8U , 1);
	cvCvtColor(greyCornersImage , greyCornersImage_gs, CV_RGB2GRAY);
/*************************************** Test 1 ******************************************

The function will just reset/init itself.
starting point will be returned as given.
true will be returned.

*****************************************************************************************/
	startingPoint = noMeaningPoint;
	if( getStartingPoint( blackImage_gs, startingPoint , WHITE , WHITE , true ) == false ) cout << "TEST 1 FAILED" << endl;
	else if( startingPoint != noMeaningPoint  ) cout << "TEST 1 FAILED" << endl;
	else cout << "TEST 1 PASSED" << endl;
/*************************************** Test 2 ******************************************

The function will reset/init itself.
The function will return false on an all black image.
The function will return true on image with white points.

*****************************************************************************************/
	getStartingPoint( blackImage_gs, startingPoint , WHITE , WHITE , true );
	if( getStartingPoint( blackImage_gs, startingPoint , WHITE , WHITE , false ) == true ) cout << "TEST 2 FAILED" << endl;
	else{
		getStartingPoint( whiteImage_gs, startingPoint , WHITE , WHITE , true );
		if( getStartingPoint( whiteImage_gs, startingPoint , WHITE , WHITE , false ) == false  ) cout << "TEST 2 FAILED" << endl;
		else cout << "TEST 2 PASSED" << endl;
	}
/*************************************** Test 3 ******************************************

The function will reset/init itself.
The function will return the point 0,0 on an all white image.

*****************************************************************************************/
	startingPoint = noMeaningPoint;
	getStartingPoint( whiteImage_gs, startingPoint , WHITE , WHITE , true );
	getStartingPoint( whiteImage_gs, startingPoint , WHITE , WHITE , false );
	if( startingPoint != initialPoint ) cout << "TEST 3 FAILED" << endl;
	else cout << "TEST 3 PASSED" << endl;
/*************************************** Test 4 ******************************************

The function will reset/init itself.
The function will return the point 0,0 on the first use with a black image with two
white corners at its top.
will return 100,0 on second use.
last starting point will be given as parameter.

*****************************************************************************************/
	startingPoint = noMeaningPoint;
	getStartingPoint( whiteCornersImage_gs, startingPoint , WHITE , WHITE , true );
	getStartingPoint( whiteCornersImage_gs, startingPoint , WHITE , WHITE , false );
	if( startingPoint != initialPoint ) cout << "TEST 4 FAILED" << endl;
	else{
		getStartingPoint( whiteCornersImage_gs, startingPoint , WHITE , WHITE , false );
		if( startingPoint != corner_0_99 ) cout << "TEST 4 FAILED" << endl;
		else cout << "TEST 4 PASSED" << endl;
	}
/*************************************** Test 5 ******************************************

The function will now be able to work two times one after the other
without last point been given.
checking the reseting has new functionality that is working correctly.

*****************************************************************************************/
	startingPoint = noMeaningPoint;
	getStartingPoint( whiteCornersImage_gs, startingPoint , WHITE , WHITE , true );
	getStartingPoint( whiteCornersImage_gs, startingPoint , WHITE , WHITE , false );
	if( startingPoint != initialPoint ) cout << "TEST 5 FAILED" << endl;
	else{
		startingPoint = noMeaningPoint;
		getStartingPoint( whiteCornersImage_gs, startingPoint , WHITE , WHITE , false );
		if( startingPoint != corner_0_99 ) cout << "TEST 5 FAILED" << endl;
		else cout << "TEST 5 PASSED" << endl;
	}
/*************************************** Test 6 ******************************************

The function will reset/init itself.
The function will now use different thresholds.
The function will return the point 0,0 on the first use with a white image with two
black corners at its top.
will return 100,0 on second use.

*****************************************************************************************/
	startingPoint = noMeaningPoint;
	getStartingPoint( blackCornersImage_gs, startingPoint , BLACK , BLACK , true );
	getStartingPoint( blackCornersImage_gs, startingPoint , BLACK , BLACK , false );
	if( startingPoint != initialPoint ) cout << "TEST 6 FAILED" << endl;
	else{
		startingPoint = noMeaningPoint;
		getStartingPoint( blackCornersImage_gs, startingPoint , BLACK , BLACK , false );
		if( startingPoint != corner_0_99 ) cout << "TEST 6 FAILED" << endl;
		else cout << "TEST 6 PASSED" << endl;
	}
/*************************************** Test 7 ******************************************

The function will reset/init itself.
The function will now use different thresholds.
The function will return the point 0,0 on the first use with a black image with two
gray corners at its top - will deal with range of grayscale.
will return 100,0 on second use.

*****************************************************************************************/
	startingPoint = noMeaningPoint;
	getStartingPoint( greyCornersImage_gs, startingPoint , BLACK+1 , WHITE-1 , true );
	getStartingPoint( greyCornersImage_gs, startingPoint , BLACK+1 , WHITE-1 , false );
	if( startingPoint != initialPoint ) cout << "TEST 7 FAILED" << endl;
	else{
		startingPoint = noMeaningPoint;
		getStartingPoint( greyCornersImage_gs, startingPoint , BLACK+1 , WHITE-1 , false );
		if( startingPoint != corner_0_99 ) cout << "TEST 7 FAILED" << endl;
		else cout << "TEST 7 PASSED" << endl;
	}

	cin >> waitForKey;
}